package cc.willrock.algorithm.sort;

import java.util.Arrays;

public class CountSort {

	/*
	 * 最好O(n)) 平均O(n) 最坏O(n)
	 */
	public static int []sort(int[] a,int max){
		int[] b =new int[max+1];
		for(int i=0;i<a.length;i++){
			b[a[i]]++;
		}
		
		int idx = 0;
		for(int i=0;i<b.length;i++){
			while(b[i]>0){
				a[idx++]=i;
				b[i]--;
			}
		}
		return a;
	}
	public static void main(String[] args) {
		int[] a = {15,9,8,1,4,11,7,12,13,6,5,3,16,2,10,14};
		//int[] a = { 5, 3, 1, 2, 4 };
		System.out.println(Arrays.toString(a));
		System.out.println(Arrays.toString(sort(a,16)));

	}

}
